//yi引入API请求函数
import {reqAddOrUpdateShopCart, reqGoodsInfo} from '@/api' 
const state = {
  goodsInfo : {},
};
const mutations = {
  GETGOODSINFO(state,data){
    state.goodsInfo = data;
  }
};
const actions = {
  // 获取产品信息action
  async getGoodsInfo({commit},skuId){
    let result = await reqGoodsInfo(skuId);
    if(result.code==200){
      commit('GETGOODSINFO',result.data) 
    }
  },
  // 添加商品到购物车或更新商品
  async addOrUpdateShopCart({commit},{skuId,skuNum}){
    // 加入购物车返回的结果
    let result= await reqAddOrUpdateShopCart(skuId,skuNum);
    if(result.code==200){
      return "ok"
    }else{
      return Promise.reject(new Error("fail"))
    }
  }
};
// 简化数据
const getters = {
  // 路径导航
  categoryView:(state)=>{
    return state.goodsInfo.categoryView||{}
  },
  // 产品信息
  skuInfo:(state)=>{
    return state.goodsInfo.skuInfo||{}
  },
  // 售卖属性
  spuSaleAttrList:(state)=>{
    return state.goodsInfo.spuSaleAttrList||[]
  }
};

export default {
  state,
  mutations,
  actions,
  getters
}